//
//  KZSearchProductCell.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/26.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZSearchProductCell: UITableViewCell {
    
    let iconView = UIImageView()
    let nameLabel = UILabel()       // 名字
    let priceLabel = UILabel()      // 现价
    let oldPriceLabel = UILabel()   // 原价
    let noteLabel = UILabel()       // 备注
    let salesLabel = UILabel()      // 已售
    
    let drawQuanButton = UIButton()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.backgroundColor = UIColor.clear
        setupUI()
    }    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

// MARK: ------ 创建ui
extension KZSearchProductCell {
    
    private func setupUI() {
        
        
        let bgView = UIImageView()
        bgView.isUserInteractionEnabled = true
        bgView.image = #imageLiteral(resourceName: "搜索-商品边框")
        
        contentView.addSubview(bgView)
        bgView.snp.makeConstraints { (m) in
            m.left.equalTo(W(4))
            m.right.equalTo(-W(5))
            
            if kIsIPhoneX {
                m.height.equalTo(106)
            } else {
                m.height.equalTo(H(106))
            }
            
            m.top.equalTo(0)
            m.bottom.equalTo(0)
        }
        
        
        bgView.addSubview(iconView)
        iconView.image = kDefaultImage
        iconView.snp.makeConstraints { (m) in
            m.left.equalTo(W(10))
            m.top.equalTo(H(10))
            m.width.equalTo(W(85))
            m.height.equalTo(iconView.snp.width)

        }
        
        nameLabel.textColor = UIColor.black
        nameLabel.numberOfLines = 2
        nameLabel.textAlignment = .left
        nameLabel.text = "智能心率手环、手表(连接手机)qqqqqqqqqqqqq"
        nameLabel.font = UIFont.systemFont(ofSize: 14)
        bgView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (m) in
            m.left.equalTo(iconView.snp.right).offset(W(13))
            m.right.equalTo(bgView.snp.left).offset(W(278))
            m.top.equalTo(iconView)
        }
        
        noteLabel.textColor = kThemeColor
        noteLabel.layer.cornerRadius = 2
        noteLabel.layer.masksToBounds = true
        noteLabel.layer.borderColor = kThemeColor.cgColor
        noteLabel.layer.borderWidth = 1
        noteLabel.adjustsFontSizeToFitWidth = true
        noteLabel.textAlignment = .center
        noteLabel.text = "1元无条件券"
        noteLabel.font = UIFont.systemFont(ofSize: 8)
        bgView.addSubview(noteLabel)
        noteLabel.snp.makeConstraints { (m) in
            m.left.equalTo(nameLabel)
            m.height.equalTo(15)
            m.width.equalTo(noteLabel.text!.width(fontSize: 8) + 20)
            m.bottom.equalTo(iconView)
        }

        salesLabel.textColor = UIColor.lightGray
        salesLabel.textAlignment = .left
        salesLabel.text = "已售253件"
        salesLabel.font = UIFont.systemFont(ofSize: 12)
        bgView.addSubview(salesLabel)
        salesLabel.snp.makeConstraints { (m) in
            m.left.equalTo(noteLabel.snp.right).offset(5)
            m.height.top.equalTo(noteLabel)
        }
        
        
        priceLabel.textColor = kThemeColor
        priceLabel.textAlignment = .left
        priceLabel.text = "￥125.0"
        priceLabel.font = UIFont.systemFont(ofSize: 16)
        bgView.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { (m) in
            m.left.equalTo(nameLabel)
            m.height.equalTo(20)
            m.bottom.equalTo(noteLabel.snp.top).offset(0)
        }
        
        oldPriceLabel.textColor = UIColor.lightGray
        oldPriceLabel.textAlignment = .left
        oldPriceLabel.text = "￥188.0"
        oldPriceLabel.font = UIFont.systemFont(ofSize: 12)
        bgView.addSubview(oldPriceLabel)
        oldPriceLabel.snp.makeConstraints { (m) in
            m.left.equalTo(priceLabel.snp.right).offset(5)
            m.bottom.height.equalTo(priceLabel)
        }
        oldPriceLabel.attributedText = oldPriceLabel.text?.attrubutesString(attrubutes: [NSAttributedStringKey.strikethroughStyle: 2])

        
        drawQuanButton.setBackgroundImage(#imageLiteral(resourceName: "搜索-领购券"), for: .normal)
        drawQuanButton.setTitle("领购券", for: .normal)
        drawQuanButton.setTitleColor(UIColor.white, for: .normal)
        drawQuanButton.titleLabel?.font = kFont(9)
        bgView.addSubview(drawQuanButton)
        drawQuanButton.snp.makeConstraints { (m) in
            if !kIsIPhoneX {
                m.height.equalTo(H(20))
                m.width.equalTo(drawQuanButton.snp.height).multipliedBy(114.0/40)
            }
            m.centerY.equalTo(bgView)
            m.centerX.equalTo(W(290 + 38))
        }
       
        
        self.layoutIfNeeded()
        
        // 虚线
        let dottedLayer = CAShapeLayer()
        dottedLayer.lineWidth = 2
        dottedLayer.strokeColor = kBgColor.cgColor
        dottedLayer.lineDashPattern = [10, 2]
        let dotPath = UIBezierPath()
        dotPath.move(to: CGPoint(x: W(289), y: H(0)))
        dotPath.addLine(to: CGPoint(x: W(289), y: H(100)))
        dottedLayer.path = dotPath.cgPath
        bgView.layer.addSublayer(dottedLayer)
        
    }
}
